define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Proconsultservice, GoPage, Auth, GoCatTypeAuth,GoDialog,$stateParams,  $window, $timeout, $ionicSideMenuDelegate) {
		if($scope.input == undefined){
			$scope.input = new Object();
		}
		$scope.input.key = "";
		$scope.input.nPage = 1;
		$scope.input.title = "";
		var cat_type_id = "";
		$scope.order = 1;
		$scope.video_type = $stateParams.video_type;
		if($scope.video_type == 1){
			$scope.input.title = "产品功能";
		}else if($scope.video_type == 2){
			$scope.input.title = "安装演示";
		}else{
			$scope.input.title = "维修演示";
		}

		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
		}, false);

		$scope.isLoad = false; //可以加载
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			Proconsultservice.getlist($scope.input.key,cat_type_id,$scope.order,$scope.video_type,1, 10,function(data) {
				if(data!=null){
					$scope.list = data.video;
				}else{
					$scope.list = new Object();
				}
				$scope.input.nPage = 1;
			},
			function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			Proconsultservice.getlist($scope.input.key,cat_type_id,$scope.order,$scope.video_type,$scope.input.nPage+1, 10,function(data) {
				if(data!=null){
					if($scope.list!=undefined){
						$scope.list = $scope.list.concat(data.video);
						$scope.input.nPage += 1;
					}
					else{
						$scope.list = data.video;
					}
				}
			},
			function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);

			});
		};

		//侧边菜单
		$scope.rightSideMenu = function() {
			$ionicSideMenuDelegate.toggleRight();
		};
		//查询
		$scope.doSearch = function() {
			if($scope.input.cat3 != null){
				cat_type_id = $scope.input.cat3.id;
			}else if($scope.input.cat2 != null){
				cat_type_id = $scope.input.cat2.id;
			}else if($scope.input.cat1 != null){
				cat_type_id = $scope.input.cat1.id;
			}else{
			    cat_type_id="";
			}
			$scope.order = 1;
			Proconsultservice.getlist($scope.input.key,cat_type_id,$scope.order,$scope.video_type, 1,10,function(data){
				$scope.list = data.video;
			});
			$ionicSideMenuDelegate.toggleRight();
		};
		//取消
		$scope.doCancel = function() {
			$ionicSideMenuDelegate.toggleRight();
		};

		//加载数据
		$scope.data_init = function(){
			Proconsultservice.getlist($scope.input.key,cat_type_id,$scope.order, $scope.video_type, 1,10,function(data) {
					$scope.list = data.video;
			});
		}
		$scope.data_init();
		
		//页面跳转
		$scope.onshow_stardetail = function(id) {
			GoPage.go_states("stardetail", { 'detail_id': id });
		}

		//综合排序
		$scope.orderby_default = function(){
			$scope.input.nPage = 1;
			if($scope.order == 1){
				$scope.order = 2;
			}else{
				$scope.order = 1;
			}
			Proconsultservice.getlist($scope.input.key,cat_type_id,$scope.order, $scope.video_type, 1,10,function(data) {
					$scope.list = data.video;
			});
			if($ionicSideMenuDelegate.isOpen()){
				//关闭侧边菜单
				$ionicSideMenuDelegate.toggleRight();
			}
		}

		//发布时间排序
		$scope.orderby_new = function(){
			$scope.input.nPage = 1;
			if($scope.order == 3){
				$scope.order = 4;
			}else{
				$scope.order = 3;
			}
			Proconsultservice.getlist($scope.input.key,cat_type_id,$scope.order, $scope.video_type, 1,10,function(data) {
					$scope.list = data.video;
			});
			if($ionicSideMenuDelegate.isOpen()){
				//关闭侧边菜单
				$ionicSideMenuDelegate.toggleRight();
			}
		}

		//播放次数排序
		$scope.orderby_count = function(){
			$scope.input.nPage = 1;
			if($scope.order == 5){
				$scope.order = 6;
			}else{
				$scope.order = 5;
			}
			Proconsultservice.getlist($scope.input.key,cat_type_id,$scope.order, $scope.video_type, 1,10,function(data) {
					$scope.list = data.video;
			});
			//关闭侧边菜单
			if($ionicSideMenuDelegate.isOpen()){
				$ionicSideMenuDelegate.toggleRight();
			}
		}
		/***分类三级联动***/
		$scope.input.cat1 = { id: 0, name: "请选择大分类" };
		$scope.input.cat2 = { id: 0, name: "请选择中分类" };
		$scope.input.cat3 = { id: 0, name: "请选择小分类" };
		//下拉侦听
		$scope.listen_cat1 = function (data) {
			$scope.input.cat2 = { id: 0, name: "请选择中分类" };
			$scope.input.cat3 = { id: 0, name: "请选择小分类" };
			$scope.cat3_items = '';
		};
		$scope.listen_cat2 = function (data) {
			$scope.input.cat3 = { id: 0, name: "请选择小分类" };
		};
		$scope.doSelectCat1 = function () {
			//id是值 name是显示
			GoDialog.showSelect("大分类", $scope.cat1_items, $scope.input.cat1, function (data) {
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function () {
			//id是值 name是显示
			GoDialog.showSelect("中分类", $scope.cat2_items, $scope.input.cat2, function (data) {
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function () {
			//id是值 name是显示
			GoDialog.showSelect("小分类", $scope.cat3_items, $scope.input.cat3, function (data) {
				$scope.input.cat3 = data;
			});
		}
		//定义绑定
		var items_cat = new Array();
		var obj1_cat = { name: 'input.cat1' };
		var obj2_cat = { name: 'input.cat2' };
		var obj3_cat = { name: 'input.cat3' };
		items_cat.push(obj1_cat);
		items_cat.push(obj2_cat);
		items_cat.push(obj3_cat);
		GoCatTypeAuth.init_load_only($scope, items_cat);
	}

	controller.$inject = ['$scope', 'Proconsultservice', 'GoPage', 'Auth', 'GoCatTypeAuth','GoDialog','$stateParams',  '$window', '$timeout', '$ionicSideMenuDelegate'];

	return app.registerController("proconsultservicectrl",controller);
});